$(function() {
    layui.use(['form','laydate','table'], function() {
            var form = layui.form, laydate = layui.laydate,
                table = layui.table;//载入table模块
            laydate.render({
                elem: '#begin_date',
                isInitValue: false,
            });
            laydate.render({
                elem: '#end_date',
                isInitValue: false,
            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                //执行搜索重载
                goSearch();
                return false;
            });
        }
    );
    goSearch();
});

function goSearch() {
    var deviceId = $("#deviceId").val();
    getRecordData(deviceId);
}


/**
 * 获取上报统计
 */
function getRecordData(deviceId) {
    var url = ctx + "module/device/charts/data";
    $.ajax({
        cache : true,
        type : "POST",
        url : url,
        data:$('#signupForm').serialize(),
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            console.log('data',data);
            var times = data.times;
            var values = data.values;
            var values1 = data.values1;
            var seriesData = [];

            var legend = ['温度', '电流'];
            if(values1!=undefined && values1.length > 0){
                legend = ['温度', '电流'];
                seriesData = [
                    {
                        type:'line',
                        name: '温度',
                        data:values,
                        itemStyle: {
                            normal: {
                                label: {
                                    show: true, //开启显示
                                    position: 'top', //在上方显示
                                    textStyle: { //数值样式
                                        color: 'blue',
                                        fontSize: 16
                                    }
                                }
                            }
                        },
                    },
                    {
                        type:'line',
                        name: '电流',
                        data:values1,
                        itemStyle: {
                            normal: {
                                label: {
                                    show: true, //开启显示
                                    position: 'top', //在上方显示
                                    textStyle: { //数值样式
                                        color: 'blue',
                                        fontSize: 16
                                    }
                                }
                            }
                        },
                    }
                ]
            }else{
                legend = ['温度'];
                seriesData = [
                    {
                        type:'line',
                        name: '温度',
                        data:values,
                        itemStyle: {
                            normal: {
                                label: {
                                    show: true, //开启显示
                                    position: 'top', //在上方显示
                                    textStyle: { //数值样式
                                        color: 'blue',
                                        fontSize: 16
                                    }
                                }
                            }
                        },
                    },
                ]
            }


            $("#begin_date").val(data.beginDate);
            $("#end_date").val(data.endDate);

            var lineChart = echarts.init(document.getElementById("echarts-line-chart"));
            var lineoption = {
                tooltip : {
                    trigger: 'axis',
                    show : true
                },
                legend: {
                    data: legend
                },
                grid:{
                    x:30,y:30,x2:5
                },
                calculable : true,
                xAxis : {
                    type: 'category',
                    data: times,
                },
                yAxis : [
                    {
                        type : 'value',
                        axisLabel : {
                            formatter: '{value}'
                        }
                    }
                ],
                series : seriesData
            };
            lineChart.setOption(lineoption);
            $(window).resize(lineChart.resize);
        }
    });
}
